const EventEmitter = require('../../common/EventEmitter');
const config = require('../../common/config');
const requester = require('../../common/requester');
const $$onLoad = require('../../appLoaded/onLoad');
const commonReq = require('../../utils/commonReq');
const formIdInit = require('../../utils/formId.js');
const shareCodeHandler = require('../../utils/shareCodeHandler');
const app = getApp();

Page({
    data: {
        isAuthed: true,
        recordList: []
    },

    onLoad: function () {
        formIdInit({
            page: this
        });
        this.pageIndexHandler('reset');
        $$onLoad()
            .then(() => {
                let isAuthed = app.globalData.isAuthed;

                this.setData({
                    isAuthed,
                    pageLoaded: true
                });
                this.init();
            });
    },

    onShow: function () {
        if (this.data.pageLoaded) {
            this.init();
        }
    },

    init: function () {
        wx.showLoading();
        this.queryPointsByPage();
    },

    queryPointsByPage: function () {
        if (this.pageIndex !== 1) {
            wx.showLoading({
                title: '加载中...'
            });
        }
        let {
            categories,
            categoryIndex,
            recordList
        } = this.data;

        let data = {
            pageIndex: this.pageIndex
        };
        return requester.request({
            url: requester.service.queryMyPointDetails,
            data: data
        }).then(res => {
            let list = res.data.pointAccountDetailVos;
            if (!list.length) {
                wx.hideLoading();
                wx.showToast({
                    title: '没有更多了~',
                    icon: 'loading',
                    duration: 1000
                });
                this.pageIndexHandler('minus');
                return;
            }
            this.setData({
                recordList: this.isLoadMore
                    ? list
                    : recordList.concat(list)
            }, () => {
                wx.hideLoading();
            });

            this.onPullDownStop();
        });
    },

    pageIndexHandler: function (type) {
        /**
         * 切换 tab
         */
        const reset = () => {
            this.pageIndex = 1
        };
        /**
         * 加载更多
         */
        const add = () => {
            this.pageIndex++
        };
        /**
         * 减 1
         */
        const minus = () => {
            this.pageIndex--
        };


        const action = {
            'reset': reset,
            'add': add,
            'minus': minus
        };

        action[type]();
    },

    /**
     * 下拉刷新
     */
    onPullDownRefresh: function () {
        console.log("onPullDownRefresh.....");
        this.isLoadMore = true;
        this.pageIndexHandler('reset');
        this.queryPointsByPage()
            .then(res => {
                this.isLoadMore = false;
            });
    },

    /***
     * 下拉刷新停止
     */
    onPullDownStop: function () {
        console.log("onPullDownStop.....");
        wx.stopPullDownRefresh();
    },

    /***
     * 滑动到底部，加载更多
     */
    onReachBottom: function () {
        console.log("onReachBottom.....");
        this.pageIndexHandler('add');
        this.queryPointsByPage();
    },

    onShareAppMessage: function (res) {
        return shareCodeHandler.getShareDetail();
    }
});
